package com.yhn.bubblevideo.admin.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.bubblevideo.admin.mapper.DanmakuMapper;
import com.yhn.bubblevideo.admin.service.DanmakuService;
import com.yhn.bubblevideo.common.enums.BaseErrorCode;
import com.yhn.bubblevideo.common.exception.BusinessException;
import com.yhn.bubblevideo.common.exception.ThrowUtils;
import com.yhn.bubblevideo.model.domain.Danmaku;
import com.yhn.bubblevideo.model.dto.req.common.DeleteRequest;
import com.yhn.bubblevideo.model.dto.req.danmaku.DanmakuAddRequestDTO;
import com.yhn.bubblevideo.model.dto.req.danmaku.DanmakuEditRequestDTO;
import com.yhn.bubblevideo.model.dto.req.danmaku.DanmakuQueryRequestDTO;
import com.yhn.bubblevideo.model.dto.resp.DanmakuRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.yhn.bubblevideo.common.constant.CommonConstant.MAX_PAGE_SIZE;

/**
 * @author yhnyhn123
 * @description 针对表【danmaku(弹幕表)】的数据库操作Service实现
 * @createDate 2024-11-21 13:33:34
 */
@Service
public class DanmakuServiceImpl extends ServiceImpl<DanmakuMapper, Danmaku>
        implements DanmakuService {

    /**
     * 分页查询
     */
    @Override
    public Page<DanmakuRespDTO> queryPage(DanmakuQueryRequestDTO requestParam) {
        if (requestParam == null) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long current = requestParam.getCurrent();
        long size = requestParam.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > MAX_PAGE_SIZE, BaseErrorCode.PARAMS_ERROR);
        Page<Danmaku> danmakuPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(requestParam));
        Page<DanmakuRespDTO> danmakuRespPage = new Page<>(current, size, danmakuPage.getTotal());
        List<DanmakuRespDTO> danmakuVO = this.getDanmakuVO(danmakuPage.getRecords());
        danmakuRespPage.setRecords(danmakuVO);
        return danmakuRespPage;
    }

    @Override
    public LambdaQueryWrapper<Danmaku> getQueryWrapper(DanmakuQueryRequestDTO requestParam) {

        if (requestParam == null) {
            return null;
        }

        LambdaQueryWrapper<Danmaku> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper
//                .like(requestParam.getDanmakuName() != null, Danmaku::get, requestParam.getName());
        return lambdaQueryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param danmakuList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<DanmakuRespDTO> getDanmakuVO(List<Danmaku> danmakuList) {
        if (CollUtil.isEmpty(danmakuList)) {
            return new ArrayList<>();
        }
        return danmakuList.stream().map(this::getDanmakuVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param danmaku 原对象
     * @return 脱敏对象
     */
    @Override
    public DanmakuRespDTO getDanmakuVO(Danmaku danmaku) {
        if (danmaku == null) {
            return null;
        }
        DanmakuRespDTO danmakuResp = new DanmakuRespDTO();
        BeanUtils.copyProperties(danmaku, danmakuResp);
        return danmakuResp;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public DanmakuRespDTO getInfoById(Long id) {
        Danmaku danmaku = baseMapper.selectById(id);
        if (danmaku == null) {
            throw new BusinessException(BaseErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getDanmakuVO(danmaku);
    }

    /**
     * 保存信息
     *
     * @param requestParam 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveDanmaku(DanmakuAddRequestDTO requestParam) {
        if (requestParam == null) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        LambdaQueryWrapper<Danmaku> queryWrapper = Wrappers.lambdaQuery(Danmaku.class)

                .eq(Danmaku::getIsDelete, 0);
        baseMapper.selectCount(queryWrapper);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("该题目已存在");
        }
        Danmaku danmaku = new Danmaku();
        BeanUtils.copyProperties(requestParam, danmaku);
        boolean result = this.save(danmaku);
        ThrowUtils.throwIf(!result, BaseErrorCode.OPERATION_ERROR);
        return danmaku.getId();
    }


    /**
     * 修改信息
     *
     * @param requestParam 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateDanmaku(DanmakuEditRequestDTO requestParam) {
        if (requestParam == null || requestParam.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        Danmaku danmaku = new Danmaku();
        BeanUtils.copyProperties(requestParam, danmaku);
        long id = requestParam.getId();
        // 判断是否存在
        Danmaku oldDanmaku = this.getById(id);
        ThrowUtils.throwIf(oldDanmaku == null, BaseErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(danmaku);
        ThrowUtils.throwIf(!result, BaseErrorCode.OPERATION_ERROR);
        return true;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }
}




